<?php

class GoodsCommentController extends FrontController{
	
	/**
	 * actionList 列表
	 * actionReply 回复
	 * actionEdit 创建
	 */
	public function actionList($title, $type, $id) {
		$model = new GoodsComment('search');
		$model->target_object_id = $id;
		$model->target_object_type = $type;
		
		if (isset($_GET['GoodsComment'])) {
			$model->attributes = $_GET['GoodsComment'];
		}
		
		$this->renderPartial('list',array(
			'title' => $title,
			'data' => $model->commentSearch(5),
			'model' => $model
		));
	}
	
	public function actionReply($id) {
		$model = new GoodsComment();
		$model->parent_id = $id;
		if (isset($_POST['GoodsComment'])) {
			$model->attributes = $_POST['GoodsComment'];
			if ($model->save()) {
				$this->redirect(array('list'));
			}
		}
		$this->render('edit',array(
			'model' => $model,
		));
	}
	
	public function actionEdit($orderId, $type, $id) {
		$model = new GoodsComment();
		$model->target_object_id = $id;
		$model->target_object_type = $type;
		$model->order_id = $orderId;
		if (isset($_POST['GoodsComment'])) {
			$model->attributes = $_POST['GoodsComment'];
			if ($model->save()) {
				if(Yii::app()->request->isAjaxRequest) {
					echo CJSON::encode(array('error'=>''));
				}
			} else {
				if(Yii::app()->request->isAjaxRequest) {
					echo CJSON::encode(array('error'=>CHtml::errorSummary($model)));
				}
			}
			die;
		}
		if(Yii::app()->request->isAjaxRequest) {
			$this->renderPartial('edit',array(
				'model' => $model,
			));
		} else {
			$this->render('edit',array(
				'model' => $model,
			));
		}
	}
	
}

?>